package com.cet.algorithm.数组;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: 顺时针打印矩阵
 * @author: 陈恩涛
 * @create: 2022-03-11 10:20
 **/
public class LC54 {
    public static List<Integer> spiralOrder(int[][] matrix) {
        int y = matrix.length;
        int x = matrix[0].length;
        int top = 0;
        int right = x - 1;
        int bottom = y - 1;
        int left = 0;
        int index = 0;
        List<Integer> result = new ArrayList<>();
        while (index < x * y) {
            for (int i = left; i <= right && index < x * y; i++) {
                result.add(matrix[top][i]);
                index++;
            }
            top++;
            for (int i = top; i <= bottom && index < x * y; i++) {
                result.add(matrix[i][right]);
                index++;
            }
            right--;
            for (int i = right; i >= left && index < x * y; i--) {
                result.add(matrix[bottom][i]);
                index++;
            }
            bottom--;
            for (int i = bottom; i >= top && index < x * y; i--) {
                result.add(matrix[i][left]);
                index++;
            }
            left++;
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] tset = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
        spiralOrder(tset).stream().forEach(System.out::println);
    }
}
